package com.seari.c3gallery.client.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import org.apache.commons.lang3.StringUtils;

public class ResCmdHandler extends SimpleChannelInboundHandler<DatagramPacket> {


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket rescmd) throws Exception {
        ByteBuf content = rescmd.content();
        String receive = content.toString(CharsetUtil.US_ASCII);
        int index = StringUtils.indexOf(receive, "K",receive.length() - 2);
        if (index < 0 ){
            //fail
        } else {
            // success
        }

        System.out.println(receive);

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
        cause.printStackTrace();
    }

    public static void main(String[] args) {

    }

}
